﻿<%@ Page Title="" Language="C#" MasterPageFile="~/GuestLayout.master" AutoEventWireup="true"
    CodeBehind="PostThread.aspx.cs" Inherits="ChatForum.Client.PostThread" %>

<asp:Content ID="Content1" ContentPlaceHolderID="mainContent" runat="server">
    <h2>
        <asp:Label ID="categoryLabel" Text='<%#Eval("CategoryName") %>' runat="server" Style="margin: 2px 2px 10px 2px" /></h2>
    <div id="postThreadListDiv">
        <asp:ListView ID="postListView" runat="server" ClientIDMode="Predictable">
            <ItemTemplate>
                <table style="width: 100%; vertical-align: sub">
                    <tr>
                        <td style="width: 10%;" rowspan="2">
                            <asp:Label ID="postCreatedByText" Text='<%#Eval("PostCreatedBy") %>' runat="server"
                                Style="margin: 2px 2px 10px 2px" />
                            <asp:ImageButton ID="onlineImage" Width="20" Height="20" runat="server" ImageUrl='<%# (bool)Eval("CreatorOnline") ? "~/images/online.png" : "~/images/offline.png" %>'
                                OnClientClick="Chat.ShowPopup(GetTargetUser(this)); return false;" Visible='<%# (bool)Eval("OnlineImageVisible") && ReplyButtonVisibility%>' />
                            <br />
                        </td>
                        <td style="width: 80%; text-align: right;">
                            <asp:Label ID="Label1" Text='<%#Eval("CreateDate") %>' runat="server" Style="margin: 0px 0px 10px 0px" />
                            <br />
                            <asp:TextBox Style='<%# "resize: none; width: " + Eval("Width") + "%" %>' Height="50px"
                                ReadOnly="true" Text='<%#Eval("Description") %>' runat="server" TextMode="MultiLine" />
                        </td>
                        <td style="width: 10%; vertical-align: bottom">
                            <asp:ImageButton Width="30px" Height="30px" ImageUrl="~/images/reply.png" ID="ReplyRequest"
                                Style="margin-left: 10px" Visible='<%#ReplyButtonVisibility%>' Text="Reply" OnClientClick="Post.ShowReplyPopup(this, event);return false;"
                                runat="server" />
                            <asp:HiddenField ID="hdnPostId" runat="server" Value='<%#Eval("PostId")%>' />
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:ListView>
    </div>
    <div class="dynamicDiv" id="replyDiv">
        <asp:HiddenField ID="hdnCurrentPostId" runat="server" />
        <div style="text-align: right;">
            <asp:Button ID="hdnReplyButton" OnClick="OnReplyPost" Style="display: none" runat="server" />
            <asp:ImageButton ImageUrl="~/images/close.png" OnClientClick="Post.HideReplyPopup(); return false;"
                Height="30px" Width="30px" runat="server" />
        </div>
        <asp:TextBox Style="border: 2px solid Black; resize: none;" ID="newPostText" Height="150px"
            onkeypress="Post.KeyInteract(event)" Width="99%" runat="server" TextMode="MultiLine" />
    </div>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="scriptContent" runat="server">
    <script type="text/javascript">
        window.onload = function PageLoad() {
            Utility.EnableDiv('postThreadListDiv');
            setInterval(Chat.ShowPopupAsynch, 3000);
        }

        ///////////////////////////////
        /// GLOBAL
        ///////////////////////////////
        var hdnPostIdFormat = 'mainContent_mainContent_postListView_hdnPostId_';
        var replyImageIdFormat = 'mainContent_mainContent_postListView_ReplyRequest_';
        var onlineImageIdFormat = 'mainContent_mainContent_postListView_onlineImage_';
        var postCreatedByTextIdFormat = 'mainContent_mainContent_postListView_postCreatedByText_';
        function GetTargetUser(sender) {
            var currentItemIndex = Utility.GetSenderIndex(sender, onlineImageIdFormat);
            return document.getElementById(postCreatedByTextIdFormat + currentItemIndex).textContent;
        }
               
        var Post = function () {

            ///////////////////////////////
            /// PRIVATE
            ///////////////////////////////
            function PostReply() {
                var replyText = document.getElementById('<%=newPostText.ClientID %>').value;
                replyText = replyText.replace(/^\s+|\s+$/g, '');
                if (replyText != null && replyText != '') {
                    document.getElementById('<%=hdnReplyButton.ClientID %>').click();
                }
                else {
                    document.getElementById('<%=newPostText.ClientID %>').value = '';
                }
            }

            ///////////////////////////////
            /// PUBLIC
            ///////////////////////////////
            return {
                ShowReplyPopup: function (sender, event) {
                    var currentItemIndex = Utility.GetSenderIndex(sender, replyImageIdFormat);

                    var hdPostIdValue = document.getElementById(hdnPostIdFormat + currentItemIndex).value;
                    document.getElementById('<%=hdnCurrentPostId.ClientID %>').value = hdPostIdValue;

                    //Clearing Reply Text Area
                    document.getElementById('<%=newPostText.ClientID %>').value = '';
                    document.getElementById('<%=newPostText.ClientID %>').focus();

                    Utility.DisableDiv('postThreadListDiv');

                    var replyDiv = document.getElementById('replyDiv');
                    replyDiv.style.top = event.clientY + 'px';
                    replyDiv.style.left = (event.clientX - 400) + 'px';
                    replyDiv.style.display = 'block';
                },
                HideReplyPopup: function () {
                    var replyDiv = document.getElementById('replyDiv');
                    replyDiv.style.display = 'none';
                    Utility.EnableDiv('postThreadListDiv');
                },
                KeyInteract: function (event) {
                    if (event.keyCode == 13 && !event.shiftKey) {
                        PostReply();
                    } else if (event.keyCode == 27) {
                        this.HideReplyPopup();
                    }
                }
            }
        } ();
    </script>
</asp:Content>
